package com.fireline.beauty.cashier;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

import com.fireline.beauty.jdbc.SqliteJdbcImpl;
import com.zgw.fireline.base.dataset.Dataset;
import com.zgw.fireline.base.widgets.TextPopup;

/**
 * 会员信息查找对话框。
 * 
 * */
public class HyxxTextPopup extends TextPopup {

	private Table table;

	private Dataset dataset;

	private boolean showSk = true; // 是否显示散客 散客编号:00000

	public HyxxTextPopup(Text control, Listener okListener) {
		super(control, okListener);
	}

	public HyxxTextPopup(Text control,boolean showSk ,Listener okListener) {
		super(control, okListener);
		this.showSk = showSk;
	}

	public void doSelectUp() {
		int index = table.getSelectionIndex() - 1;
		if (index >= 0 && index < table.getItemCount())
			table.setSelection(index);
	}

	public void doSelectDown() {
		int index = table.getSelectionIndex() + 1;
		if (index >= 0 && index < table.getItemCount())
			table.setSelection(index);
	}

	@Override
	protected void doFilter(String key) {
		table.removeAll();
		String hybh, hymc, jm;
		TableItem item;
		for (int i = 0; i < dataset.getCount(); i++) {
			dataset.setSelected(i);
			hybh = dataset.getValue("hybh", String.class, "");
			hymc = dataset.getValue("hyxm", String.class, "");
			jm = dataset.getValue("jm", String.class, "");
			if (!showSk && hybh.equals("00000")) { // 过滤散客
				continue;
			}
			if (hybh.contains(key) || hymc.contains(key) || jm.contains(key)) {
				item = new TableItem(table, SWT.NONE);
				item.setText(0, hybh);
				item.setText(1, hymc);
				item.setText(2, dataset.getValue("xb", String.class, ""));
				item.setText(3, dataset.getValue("hykmc", String.class, ""));
				item.setText(4, dataset.getValue("bz", String.class, ""));
				item.setData(i);
			}
		}
		if (table.getItemCount() > 0) {
			table.setSelection(0);
			dataset.setSelected(0);
		}
	}

	/**
	 * @wbp.parser.entryPoint
	 */
	@Override
	protected Control createContent(Composite parent) {
		Composite content = new Composite(parent, SWT.BORDER);
		GridLayout gl_content = new GridLayout();
		gl_content.verticalSpacing = 0;
		gl_content.marginWidth = 0;
		gl_content.marginHeight = 0;
		gl_content.horizontalSpacing = 0;
		content.setLayout(gl_content);
		table = new Table(content, SWT.FULL_SELECTION);

		table.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				if (table.getSelectionCount() > 0) {
					okPressed();
				}
			}
		});
		GridData gd_table = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_table.heightHint = 0;
		gd_table.verticalIndent = -1;
		table.setLayoutData(gd_table);
		table.setHeaderVisible(true);

		TableColumn tblclmnNewColumn = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn.setWidth(79);
		tblclmnNewColumn.setText("会员编号");

		TableColumn tblclmnNewColumn_1 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_1.setWidth(79);
		tblclmnNewColumn_1.setText("会员姓名");

		TableColumn tblclmnNewColumn_2 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_2.setWidth(44);
		tblclmnNewColumn_2.setText("性别");

		TableColumn tableColumn_1 = new TableColumn(table, SWT.NONE);
		tableColumn_1.setWidth(80);
		tableColumn_1.setText("会员卡名称");

		TableColumn tableColumn = new TableColumn(table, SWT.NONE);
		tableColumn.setWidth(160);
		tableColumn.setText("备注信息");

		Label label = new Label(content, SWT.SEPARATOR | SWT.HORIZONTAL);
		label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1,
				1));

		Composite composite = new Composite(content, SWT.NONE);
		RowLayout rl_composite = new RowLayout(SWT.HORIZONTAL);
		rl_composite.marginTop = 0;
		rl_composite.spacing = 10;
		composite.setLayout(rl_composite);
		GridData gd_composite = new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1);
		gd_composite.verticalIndent = 5;
		gd_composite.horizontalIndent = -1;
		composite.setLayoutData(gd_composite);

		Button btnNewButton_1 = new Button(composite, SWT.NONE);
		btnNewButton_1.setLayoutData(new RowData(80, SWT.DEFAULT));
		btnNewButton_1.setText("选择&S");

		Button btnNewButton = new Button(composite, SWT.NONE);
		btnNewButton.setLayoutData(new RowData(80, SWT.DEFAULT));
		btnNewButton.setText("关闭&C");
		setParams();
		return content;
	}

	/**
	 * 刷新数据
	 * */
	public void refreshData() {
		dataset = new Dataset("select *,sr||csrq hysr from hyxx",
				SqliteJdbcImpl.INSTANCE);
		dataset.setKeyColumns(new String[] { "hybh" });
		dataset.update();
	}

	@Override
	public void show(String trim) {
		if (!isShow()) { // 显示界面时刷新数据
			refreshData();
		}
		super.show(trim);
	}

	/**
	 * 根据会员编号，或取会员信息
	 * */
	public Dataset getHyxx(String hybh) {
		dataset.setSelected(hybh);
		if (dataset.getSelectedIndex() != -1) {
			return dataset;
		}
		return null;
	}

	@Override
	protected void okPressed() {
		if (table.getSelectionCount() > 0) {
			int index = (Integer) table.getSelection()[0].getData();
			dataset.setSelected(index);
			hide();
			Event event = new Event();
			event.data = dataset;
			okListener.handleEvent(event);
		} else {
			MessageDialog.openInformation(getShell(), "提示", "请选择一条记录");
		}
	}

	protected void setParams() {
	}

	@Override
	protected Point getSize() {
		return new Point(450, 200);
	}
}
